#include "SettingsWidget.h"
#include "ui_SettingsWidget.h"
#include <QFileDialog>
#include <ChangePassDialog.h>
#include <INoteView.h>

SettingsWidget::SettingsWidget(QWidget *parent) :
	QWidget(parent),
	ui(new Ui::SettingsWidget),
	_provider(NULL)
{
	ui->setupUi(this);

	connect(ui->btnDBLocation,SIGNAL(clicked()),SLOT(onBtnDBLocation()));
	connect(ui->btnHidePanel,SIGNAL(clicked()),SIGNAL(hidePanel()));
	connect(ui->btnAcceptPass,SIGNAL(clicked()),SLOT(onBtnAcceptPass()));
	connect(ui->btnChangePass,SIGNAL(clicked()),SLOT(onBtnChangePass()));
	connect(ui->checkShowDlg,SIGNAL(toggled(bool)),SLOT(onShowDlg(bool)));
	connect(ui->comboCipher,SIGNAL(currentIndexChanged(int)),SLOT(onCipherTypeChanged(int)));

	ui->comboCipher->insertItem(0,tr("None"),CipherType::None);
	ui->comboCipher->insertItem(1,"RC4",CipherType::RC4);
	ui->comboCipher->insertItem(2,"DES",CipherType::DES);
	ui->comboCipher->insertItem(3,"AES",CipherType::AES);

	ui->btnAcceptPass->setIcon(QIcon(":/png/rsc/apply_arrow.png"));
	ui->btnHidePanel->setStyleSheet(INoteView::getButtonStyle("hide"));
}

SettingsWidget::~SettingsWidget()
{
	delete ui;
}

void SettingsWidget::setDataProvider(DataProvider* provider)
{
	_provider = provider;
	ui->checkShowDlg->setChecked(_provider->settings()->showDlgAtStartup);
	updatePass(_provider->settings()->userType,"");
	QFileInfo info(_provider->settings()->database);
	ui->btnDBLocation->setToolTip(tr("Current database: ") + info.fileName());
}

void SettingsWidget::onBtnDBLocation()
{
	if(!_provider){
		/// TODO: database not loaded message
		_provider->showMessage(tr("Can't load database. Internal error."),5000);
		return;
	}
	QFileInfo prev(_provider->getDatabase()->getCurrentFile());
	QString filename = QFileDialog::getOpenFileName(0, tr("Select database location"), prev.filePath(), tr("DataBase (*.mdb);;All Files (*.*)"));
	if(!filename.isEmpty()){
		QFile file(filename);
		if(!file.exists()){
			_provider->showMessage(tr("File ") + filename + tr(" doesn't exist."), 5000);
			return;
		}
		QFileInfo info(file);
		if(info.suffix() != "mdb"){
			_provider->showMessage(tr("File ") + filename + tr(" has wrong suffix."), 5000);
			return;
		}

		if(!_provider->getDatabase()->setDataBase(filename)){
			_provider->showMessage(tr("Database ") + filename + tr(" has wrong type."), 5000);
			return;
		}
		_provider->loadDatabase();
		ui->btnDBLocation->setToolTip(tr("Current database: ") + info.fileName());
		_provider->settings()->database = filename;
	}
}

void SettingsWidget::updatePass(CipherType::Enum type, QString str)
{
	int index = ui->comboCipher->findData(type);
	if(index != -1){
		ui->comboCipher->setCurrentIndex(index);
		ui->editPass->setEnabled(type != CipherType::None);
	}

	ui->editPass->setText(str);
}

void SettingsWidget::onBtnAcceptPass()
{
	if(ui->editPass->text().isEmpty()){
		updatePass(CipherType::None,"");
	}

	CipherType::Enum type = (CipherType::Enum) ui->comboCipher->itemData(ui->comboCipher->currentIndex()).value<int>();
	_provider->getCrypt()->setPassword(type, ui->editPass->text());
	_provider->loadDatabase();
	_provider->settings()->userType = type;
}

void SettingsWidget::onBtnChangePass()
{
	ChangePassDialog dlg(_provider, this);
	connect(&dlg, SIGNAL(passChaged(CipherType::Enum,QString)),SLOT(updatePass(CipherType::Enum,QString)));
	CipherType::Enum type = (CipherType::Enum) ui->comboCipher->itemData(ui->comboCipher->currentIndex()).value<int>();
	dlg.setCurrentPass(type, ui->editPass->text());
	dlg.exec();
}

void SettingsWidget::onShowDlg(bool state)
{
	_provider->settings()->showDlgAtStartup = state;
}

void SettingsWidget::onCipherTypeChanged(int index)
{
	if(index == -1) return;
	CipherType::Enum type = (CipherType::Enum) ui->comboCipher->itemData(index).value<int>();
	ui->editPass->setEnabled(type != CipherType::None);
}
